<?php

class IcaraDataHandler {

  private static $resources;
  protected $driver;
  protected $type;

  const STORE   = 0;
  const RELATIONAL = 1;
  const SERVICE    = 2;
  const FILESYSTEM = 4;
  const CACHE      = 8;

  public function isNoSql() {
    return ($this->type == $this->STORE);
  }

  public function isService() {
    return ($this->type == $this->SERVICE);
  }

  public function isRelational() {
    return ($this->type == $this->RELATIONAL);
  }

  public function __construct($uri) {
    if(!is_array($this->resources)) {
      $this->resources = array();
    }
    if(!isset($this->resources[$uri])) {
      $this->driver = self::res($uri);
      $this->resources[$uri] = $this->driver;
    } else {
      $this->driver = &$this->resources[$uri];
    }
  }

  public function getStoreApi() {
    return IcaraStoreApi($this->driver->driver,$this->driver->name,$this->driver->params);
  }

  public function getDatabaseApi() {
    return IcaraDatabaseApi($this->driver->driver,$this->driver->name,$this->driver->params);
  }
  
  public function getFilesystemApi() {
    return IcaraDatabase($this->driver->driver,$this->driver->name,$this->driver->params);
  }

  public function getServiceApi() {
    return IcaraDatabase($this->driver->driver,$this->driver->name,$this->driver->params);
  }
  
  public function getCacheApi() {
    return IcaraDatabase($this->driver->driver,$this->driver->name,$this->driver->params);
  }

  private static function res($string) {
    $info = parse_url($string);
    $scheme = $info['scheme'];

    $db = new stdClass();
    $db->KEYVALUE   = 0;
    $db->RELATIONAL = 1;
    $db->SERVICE    = 2;
    $db->name       = $scheme;
    $db->params     = parse_str($scheme['query']);

    switch($scheme) {

      case 'mysql' : 
        $db->driver = new PDO(
           'mysql:host='.$info['host'].';port='.
           (!isset($info['port']) ? $info['port'] : '3306').
           ';dbname='.$info['path'],
           $info['user'],
           $info['pass'],
           array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $db->type = $driver->RELATIONAL;
        break;

      case 'oracle' : 
        $db->driver = new PDO(
           'oci:host='.$info['host'].';port='.
           (!isset($info['port']) ? $info['port'] : '1521').
           ';dbname='.$info['path'],
           $info['user'],
           $info['pass']);
        $db->type = $driver->RELATIONAL;
        break;

      case 'postgresql' : 
        $db->driver = new PDO(
           'pgsql:host='.$info['host'].';port='.
           (!isset($info['port']) ? $info['port'] : '5432').
           ';dbname='.$info['path'],
           $info['user'],
           $info['pass']);
        $db->type = $driver->RELATIONAL;
        break;

      case 'memcache' : 
        $db->driver = memcache_connect($info['host'],(!isset($info['port']) ? $info['port'] : '11211'));
        $db->type = $driver->KEYVALUE;
        break;

      case 'mongodb' : 
        $db->driver = new Mongo("mongodb://".$info['user'].":".$info['pass']."@".$info['host'].':'.
           (!isset($info['port']) ? $info['port'] : '27017'));
        $db->type = $driver->KEYVALUE;
        break;

      case 'mongodb' : 
        $db->driver = new Mongo("mongodb://".$info['user'].":".$info['pass']."@".$info['host'].':'.
           (!isset($info['port']) ? $info['port'] : '27017'));
        $db->type = $driver->KEYVALUE;
        break;

      case 'file' : 
        $db->driver = $info['path'];
        $db->type = $driver->KEYVALUE;
        break;
    }
    return $db;
  }

}

class IcaraStoreApi {
  private $resource;
  private $source;
  private $data;
  private $cached;

  public function set($key,$val){
    if($this->cached) {
      $this->data['key']=$val;
    }
  }

  public function get($key){
    if($this->cached && isset($this->data['key'])) {
      return $this->data['key'];
    }
  }

  public function __construct($resource,$sourcem,$params,$cached=false) {
    $this->source   = $source;
    $this->resource = $resource;
    $this->cached   = $cached;
    $this->data     = array();
  }
}

class IcaraFilesystemApi {
  private $resource;
  private $source;
  private $data;
  private $cached;

  public function set($key,$val){
    if($this->cached) {
      $this->data['key']=$val;
    }
  }

  public function get($key){
    if($this->cached && isset($this->data['key'])) {
      return $this->data['key'];
    }
  }

  public function __construct($resource,$source,$params,$cached=false) {
    $this->source   = $source;
    $this->resource = $resource;
    $this->cached   = $cached;
    $this->data     = array();
  }
}

class IcaraDatabaseApi {
  private $resource;
  private $source;

  public function save();
  public function load();
  public function remove();

  public function __construct($resource,$source,$params) {
    $this->source   = $source;
    $this->resource = $resource;
  }
}

